var LOGIN_SUCCESS = false;
function initSearch(c) {
	var e = $("#navigation");
	var b = e.find("a");
	var d = e.find(".nav-body form");
	b.click(function() {
		b.filter(".on").removeClass("on");
		$(this).addClass("on");
		this.blur()
	});
	d.find("input.btn").mouseover(function() {
		$(this).addClass("on")
	}).mouseout(function() {
		$(this).removeClass("on")
	});
	d.find("input[name=key]").keyhint();
	d.find("input[type=button]").click(
			function() {
				var a = d.find("input[name=key]").not(".gray").val();
				location = "/question.php?act=add"
						+ (this.name == "share" ? "&share=1" : "")
						+ (a ? "&key=" + encodeURIComponent(a) : "")
			})
}
function initLogin() {
	$("#login-form").validate( {
		onkeyup : false,
		focusInvalid : false,
		onfocusin : false,
		onfocusout : false,
		errorClass : false,
		showErrors : function() {
			var a = this.errorList[0];
			if (a) {
				a.element.focus();
				alert(a.message);
				return false
			}
		},
		submitHandler : function(a) {
			var a = $(a);
			var b = a.serializeArray();
			$.ajax( {
				type : "POST",
				dataType : "json",
				url : a.attr("action"),
				data : b,
				success : function(c) {
					if (c.success) {
						location.reload()
					} else {
						alert(c.msg)
					}
				},
				error : function(c) {
					alert(TXT_NETBUSY)
				}
			})
		},
		rules : {
			account : {
				required : true
			},
			password : {
				required : true
			}
		},
		messages : {
			account : {
				required : "\u8bf7\u8f93\u5165\u62a4\u7167\u540d"
			},
			password : {
				required : "\u8bf7\u8f93\u5165\u767b\u5f55\u5bc6\u7801"
			}
		}
	})
}
function initHome() {
	initLogin()
}
function initList() {
	initLogin()
}
function initQuestionForm(b) {
	initLogin();
	initClasses("#class-0", "#classid");
	var c = $("#form-question").submit(function() {
		return false
	});
	c.find('input[type="reset"]').click(function() {
		$("#class-0").nextAll().remove()
	});
	var a = $("#q-link").attr("href");
	var e = $("#l-link").attr("href");
	if (b) {
		c.find(":input").attr("disabled", true).addClass("disabled");
		return
	}
	var d = $("#successform").window( {
		draggable : true,
		drag : {
			handle : ".window-header"
		}
	});
	c
			.validate( {
				onkeyup : false,
				focusInvalid : false,
				onfocusin : false,
				onfocusout : false,
				errorClass : false,
				showErrors : function() {
					var f = this.errorList[0];
					if (f) {
						var g = f.element;
						if (g.id == "classid") {
							g = $("#class-0").parent().children(":last")
						}
						alert(f.message);
						g.focus()
					}
				},
				submitHandler : function(g) {
					var f = $("#upload").contents().find(":file");
					if (f.size()) {
						for (i = 0; i < f.size(); i++) {
							if (f[i].value) {
								alert("\u4f60\u6709\u672a\u4e0a\u4f20\u7684\u6587\u4ef6\uff0c\u8bf7\u5148\u70b9\u51fb\u4e0a\u4f20");
								return false
							}
						}
					}
					var g = $(g);
					var h = g.serializeArray();
					g.disabled();
					$
							.ajax( {
								type : "POST",
								dataType : "json",
								url : g.attr("action"),
								data : h,
								success : function(j) {
									if (j.success) {
										$("#q-link").attr("href",
												a.replace("$qid", j.qid));
										$("#l-link").attr(
												"href",
												e.replace("$cid", $("#classid")
														.val()));
										d.show()
									} else {
										alert(j.msg);
										g.enabled()
									}
								},
								error : function(j) {
									alert(TXT_NETBUSY);
									g.enabled()
								}
							})
				},
				rules : {
					title : {
						required : true,
						rangelength : [ 5, 100 ]
					},
					classid : {
						required : true,
						min : 1
					},
					content : {
						required : true
					},
					coin : {
						digits : true,
						max : 100
					}
				},
				messages : {
					title : {
						required : "\u8bf7\u586b\u5199\u6807\u9898",
						rangelength : $
								.format("\u6807\u9898\u957f\u5ea6\u5fc5\u987b\u4e3a{0}-{1}\u4e2a\u5b57\u7b26")
					},
					classid : "\u8bf7\u9009\u62e9\u5206\u7c7b",
					content : {
						required : "\u8bf7\u586b\u5199\u8be6\u7ec6\u5185\u5bb9"
					},
					coin : {
						digits : "\u8bf7\u586b\u5199\u6b63\u786e\u7684\u60ac\u8d4f\u82b1\u751f\u7c73",
						max : $
								.format("\u60ac\u8d4f\u82b1\u751f\u7c73\u4e0d\u5f97\u5927\u4e8e{0}")
					}
				}
			})
}
function loginModal(a) {
	if (!$("#loginmodal").size() || LOGIN_SUCCESS) {
		return true
	}
	var b = $("#modalform").unbind("submit");
	b.submit(function() {
		var d = b.serializeArray();
		$.ajax( {
			type : "POST",
			async : false,
			dataType : "json",
			url : b.attr("action"),
			data : d,
			success : function(e) {
				if (e.success) {
					LOGIN_SUCCESS = true;
					$("#loginmodal").window().close();
					if ($.isFunction(a)) {
						a()
					}
				} else {
					alert(e.msg)
				}
			},
			error : function(e) {
				alert(TXT_NETBUSY)
			}
		});
		return false
	});
	var c = $("#loginmodal").window( {
		close : false,
		show : function() {
			b.find("input[name=account]").focus()
		}
	});
	c.show();
	return false
}
function initQuestion() {
	initLogin();
	$("#replyform")
			.validate(
					{
						onkeyup : false,
						focusInvalid : false,
						onfocusin : false,
						onfocusout : false,
						errorClass : false,
						submitHandler : function(c) {
							var c = $(c);
							if (!loginModal(function() {
								c.submit()
							})) {
								return
							}
							var d = c.serializeArray();
							c.disabled();
							$.ajax( {
								type : "POST",
								dataType : "json",
								url : c.attr("action"),
								data : d,
								success : function(e) {
									if (e.success) {
										location.reload()
									} else {
										alert(e.msg);
										if (e.repeat) {
											location.reload()
										}
										c.enabled()
									}
								},
								error : function(e) {
									alert(TXT_NETBUSY);
									c.enabled()
								}
							})
						},
						showErrors : function() {
							var c = this.errorList[0];
							if (c) {
								c.element.focus();
								alert(c.message);
								return false
							}
						},
						rules : {
							content : {
								required : true,
								minlength : 5
							}
						},
						messages : {
							content : {
								required : "\u5fc5\u987b\u8f93\u5165\u56de\u590d\u7684\u5185\u5bb9",
								minlength : $
										.format("\u56de\u590d\u5185\u5bb9\u957f\u5ea6\u4e0d\u80fd\u5c11\u4e8e{0}\u4e2a\u5b57\u7b26")
							}
						}
					});
	var a = $("#rating ul.enabled");
	var b = a.find("li.current");
	a.hover(function() {
		b.hide()
	}, function() {
		b.show()
	});
	$("#rating li.current").hide().show();
	a.find("li a").click(function() {
		var d = $.func.checkInt($(this).text());
		var c = a.attr("id").split("-").pop();
		a.removeClass("enabled").unbind("mouseenter").unbind("mouseleave");
		a.find("li a").unbind("click");
		b.show();
		voteQuestion(c, d, true)
	});
	$("div.attach img").each(function() {
		resetImg(this)
	}).load(function() {
		resetImg(this)
	})
}
function validateModify(b) {
	var a = $(b)
			.validate(
					{
						onkeyup : false,
						focusInvalid : false,
						onfocusin : false,
						onfocusout : false,
						errorClass : false,
						showErrors : function() {
							var c = this.errorList[0];
							if (c) {
								c.element.focus();
								alert(c.message);
								return false
							}
						},
						submitHandler : function(d) {
							var d = $(d);
							var f = d.find('input[name="aid"]').val();
							var c = d.find('textarea[name="content"]').val();
							var e = d.serializeArray();
							d.disabled();
							$
									.ajax( {
										type : "POST",
										dataType : "json",
										data : e,
										url : d.attr("action"),
										success : function(g) {
											if (g.success) {
												$(
														"#reply-"
																+ f
																+ " div.replyer")
														.hide();
												$("#reply-" + f + " .content")
														.html(
																c
																		.replace(
																				/\</ig,
																				"&lt;")
																		.replace(
																				/\>/ig,
																				"&gt;")
																		.replace(
																				/ /ig,
																				"&nbsp;")
																		.replace(
																				/\r|\n/ig,
																				"<br />")
																		.replace(
																				/((http|https|ftp):\/\/([\w\d\-]+\.)+[\w]*\/{0,1}([\w\d\-\_]+\/)*[\x21-\x7e]+)/ig,
																				'<a href="$1" target="_blank">$1</a>'))
											} else {
												alert(g.msg)
											}
											d.enabled()
										},
										error : function(g) {
											alert(TXT_NETBUSY);
											d.enabled()
										}
									})
						},
						rules : {
							content : {
								required : true
							}
						},
						messages : {
							content : {
								required : "\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a"
							}
						}
					});
	return a
}
function voteQuestion(d, c, b, a) {
	if (!loginModal(function() {
		voteQuestion(d, c, b, true)
	})) {
		return
	}
	$
			.ajax( {
				type : "POST",
				dataType : "json",
				data : {
					qid : d,
					score : c
				},
				url : "/question.php?act=vote",
				success : function(e) {
					if (!e.success) {
						alert(e.msg)
					} else {
						if (b) {
							var j = $("#rating ul");
							var k = e.current;
							if (!k) {
								return
							}
							j.find("li.current").css("left", k.left * 40).css(
									"width", k.width * 40);
							if (k.average >= 0) {
								j.find("li.current").removeClass(
										"negative-current")
							}
							$("#curscore")
									.html(
											"<strong>"
													+ k.average
													+ '</strong>&nbsp;<em class="gray">('
													+ k.votenum
													+ '\u6b21\u8bc4\u5206)</em>&nbsp;<span class="green">'
													+ e.msg + "</span>")
						} else {
							var h = $("#agree span").html().toInt();
							var l = $("#disagree span").html().toInt();
							c > 0 ? h++ : l++;
							var g = h + l;
							var f = parseInt(h / g * 100);
							$("#agree span").html(h + "");
							$("#disagree span").html(l + "");
							$("#agree em").html(f + "%");
							$("#disagree em").html((100 - f) + "%");
							$("#votenum").html(g);
							$("#votebtns button").attr("disabled", true)
									.addClass("disabled")
						}
					}
					if (a) {
						location.reload()
					}
				},
				error : function(e) {
					alert(TXT_NETBUSY)
				}
			})
}
function showModifyForm(c, a) {
	var b = $("#reply-" + a + " .content").html();
	b = b.replace(/\r|\n/ig, "").replace(/&nbsp;/ig, " ").replace(
			/<br[^>]*>/ig, "\n");
	b = b.replace(/<[^>]+>([^<>]+)<\/[^>]+>/ig, "$1");
	if ($("#reply-" + a + " div.replyer:hidden").size()) {
		$("#reply-" + a + " div.replyer").show();
		$("#answer-form-" + a).find("textarea").val(b);
		validateModify("#answer-form-" + a)
	} else {
		$("#reply-" + a + " div.replyer").hide()
	}
}
function appendQuestion(c) {
	var a = "";
	var b = $("#question-" + c + " div.ext");
	if (a = b.html()) {
		a = a.replace(/<[^>]+>([^<>]+)<\/[^>]+>/ig, "$1");
		a = a.replace(/\r|\n/ig, "").replace(/&nbsp;/ig, " ").replace(
				/<br[^>]*>/ig, "\n");
		a = a.substr(5)
	}
	$("#append").prev().hide();
	$("#append").show().find("textarea").val(a);
	$("#append")
			.find("form")
			.validate(
					{
						onkeyup : false,
						focusInvalid : false,
						onfocusin : false,
						onfocusout : false,
						errorClass : false,
						showErrors : function() {
							var d = this.errorList[0];
							if (d) {
								d.element.focus();
								alert(d.message);
								return false
							}
						},
						submitHandler : function(d) {
							var d = $(d);
							var e = d.serializeArray();
							d.disabled();
							$
									.ajax( {
										type : "POST",
										dataType : "json",
										data : e,
										url : d.attr("action"),
										success : function(f) {
											d.enabled();
											if (f.success) {
												b
														.html("<strong>\u95ee\u9898\u8865\u5145\uff1a</strong>"
																+ $(
																		"#extcontent")
																		.val()
																		.replace(
																				/\r|\n/ig,
																				"<br />"))
											} else {
												alert(f.msg)
											}
											hideAppend()
										},
										error : function(f) {
											alert(TXT_NETBUSY);
											d.enabled()
										}
									})
						},
						rules : {
							extcontent : {
								required : true,
								rangelength : [ 5, 250 ]
							}
						},
						messages : {
							extcontent : {
								required : "\u8bf7\u8f93\u5165\u95ee\u9898\u7684\u8865\u5145\u5185\u5bb9",
								rangelength : $
										.format("\u95ee\u9898\u8865\u5145\u5185\u5bb9\u957f\u5ea6\u5fc5\u987b\u4e3a{0}-{1}\u4f4d")
							}
						}
					})
}
function hideAppend() {
	$("#append").hide();
	$("#append").prev().show()
}
function toReply() {
	this.location = "#reply";
	$("#replyform textarea").focus()
}
function initClasses(a, c) {
	var a = $(a);
	var c = $(c);
	var b = _classes;
	$.each(b[0], function() {
		a.append('<option value="' + this[0] + '">' + this[1] + "</option>")
	});
	d(a);
	function d(e) {
		e
				.change(function() {
					e.nextAll().remove();
					var f = this.value.toInt();
					c.val(f);
					if (f && b[f]) {
						var g = $('<select><option value="">\u8bf7\u9009\u62e9\u4e0b\u5c5e\u5206\u7c7b\uff08\u53ef\u9009\uff09</option></select>');
						g.css("margin-left", 6);
						g.insertAfter(e);
						$.each(b[f], function() {
							g.append('<option value="' + this[0] + '">'
									+ this[1] + "</option>")
						});
						d(g)
					}
				})
	}
}
function setBest(b) {
	var d = $("#setbest").window( {
		width : 600,
		closeCls : "close",
		draggable : true,
		drag : {
			handle : ".window-header"
		}
	});
	var a = $("#reply-" + b + " .user p").html();
	var c = $("#reply-" + b + " .content").html();
	c = cutstr(c, 100);
	$("#setbest div.post .content").html(c);
	$("#setbest div.post .user").html(a);
	$("#setbest input[name=aid]").val(b);
	initCloseForm("#setbestform");
	d.show()
}
function closeQuestion() {
	var a = $("#close").window( {
		width : 600,
		closeCls : "close",
		draggable : true,
		drag : {
			handle : ".window-header"
		}
	});
	initCloseForm("#closeform");
	a.show()
}
function initCloseForm(a) {
	a = $(a);
	if (a.attr("initialized")) {
		return
	}
	a.attr("initialized", true);
	a.submit(function() {
		var b = $(this);
		var c = b.serializeArray();
		b.disabled();
		$.ajax( {
			type : "POST",
			dataType : "json",
			url : b.attr("action"),
			data : c,
			success : function(d) {
				if (d.success) {
					location.reload()
				} else {
					alert(d.msg);
					b.enabled()
				}
			},
			error : function(d) {
				alert(TXT_NETBUSY);
				b.enabled()
			}
		});
		return false
	})
}
function copyToClipboard(l, a, f) {
	var g = f + "\r\n" + location;
	if (window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", g)
	} else {
		if (navigator.userAgent.indexOf("Opera") != -1) {
			window.location = g
		} else {
			if (window.netscape) {
				try {
					netscape.security.PrivilegeManager
							.enablePrivilege("UniversalXPConnect")
				} catch (j) {
					alert("\u88ab\u6d4f\u89c8\u5668\u62d2\u7edd\uff01\n\u8bf7\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165'about:config'\u5e76\u56de\u8f66\n\u7136\u540e\u5c06'signed.applets.codebase_principal_support'\u8bbe\u7f6e\u4e3a'true'")
				}
				var d = Components.classes["@mozilla.org/widget/clipboard;1"]
						.createInstance(Components.interfaces.nsIClipboard);
				if (!d) {
					return
				}
				var m = Components.classes["@mozilla.org/widget/transferable;1"]
						.createInstance(Components.interfaces.nsITransferable);
				if (!m) {
					return
				}
				m.addDataFlavor("text/unicode");
				var k = new Object();
				var h = new Object();
				var k = Components.classes["@mozilla.org/supports-string;1"]
						.createInstance(Components.interfaces.nsISupportsString);
				var c = g;
				k.data = c;
				m.setTransferData("text/unicode", k, c.length * 2);
				var b = Components.interfaces.nsIClipboard;
				if (!d) {
					return false
				}
				d.setData(m, null, b.kGlobalClipboard)
			}
		}
	}
	alert("\u95ee\u9898\u5730\u5740\u5df2\u590d\u5236\u5230\u526a\u5207\u7248")
}
function cutstr(c, b, a) {
	if (!a) {
		a = "..."
	}
	if (c.length > b) {
		return c.substr(0, b) + a
	}
	return c
}
function delQuestion(a) {
	if (!confirm("\u786e\u5b9a\u8981\u5220\u9664\u8fd9\u4e2a\u95ee\u9898?")) {
		return
	}
	$.ajax( {
		type : "POST",
		dataType : "json",
		url : "/manage.php",
		data : {
			act : "question",
			op : "delete",
			qid : a
		},
		success : function(b) {
			if (b.success) {
				location = "/list/c" + b.classid + "/"
			} else {
				alert(b.msg)
			}
		},
		error : function(b) {
			alert(TXT_NETBUSY)
		}
	})
}
function hideQuestion(a) {
	$.ajax( {
		type : "POST",
		dataType : "json",
		url : "/manage.php",
		data : {
			act : "question",
			op : "hide",
			qid : a
		},
		success : function(b) {
			if (b.success) {
				location.reload()
			} else {
				alert(b.msg)
			}
		},
		error : function(b) {
			alert(TXT_NETBUSY)
		}
	})
}
function delReply(b, a) {
	if (!confirm("\u786e\u5b9a\u8981\u5220\u9664\u8fd9\u4e2a\u56de\u590d?")) {
		return
	}
	$.ajax( {
		type : "POST",
		dataType : "json",
		url : "/manage.php",
		data : {
			act : "reply",
			op : "delete",
			qid : b,
			aid : a
		},
		success : function(c) {
			if (c.success) {
				location.reload()
			} else {
				alert(c.msg)
			}
		},
		error : function(c) {
			alert(TXT_NETBUSY)
		}
	})
}
function hideReply(b, a) {
	$.ajax( {
		type : "POST",
		dataType : "json",
		url : "/manage.php",
		data : {
			act : "reply",
			op : "hide",
			qid : b,
			aid : a
		},
		success : function(c) {
			if (c.success) {
				location.reload()
			} else {
				alert(c.msg)
			}
		},
		error : function(c) {
			alert(TXT_NETBUSY)
		}
	})
}
function initHelp() {
	initSearch("help");
	$("div.content img").each(function() {
		this.onclick = function() {
			window.open(this.src)
		};
		this.style.cursor = "pointer";
		resetImg(this)
	}).load(function() {
		resetImg(this)
	})
}
function addCoin() {
	var a = $("#addcoin").window( {
		width : 600,
		closeCls : "close",
		draggable : true,
		drag : {
			handle : ".window-header"
		}
	});
	$("#addcoin")
			.find("form")
			.validate(
					{
						onkeyup : false,
						focusInvalid : false,
						onfocusin : false,
						onfocusout : false,
						errorClass : false,
						showErrors : function() {
							var b = this.errorList[0];
							if (b) {
								b.element.focus();
								alert(b.message);
								return false
							}
						},
						submitHandler : function(b) {
							var b = $(b);
							var c = b.serializeArray();
							b.disabled();
							$.ajax( {
								type : "POST",
								url : b.attr("action"),
								data : c,
								dataType : "json",
								success : function(d) {
									if (d.success) {
										location.reload()
									} else {
										alert(d.msg);
										b.enabled()
									}
								},
								error : function(d) {
									b.enabled();
									alert(TXT_NETBUSY)
								}
							})
						},
						rules : {
							coin : {
								required : true
							}
						},
						messages : {
							coin : {
								required : "\u8bf7\u6b63\u786e\u9009\u62e9\u8ffd\u52a0\u60ac\u8d4f\u6570"
							}
						}
					});
	a.show()
}
function resetImg(b) {
	var a = $(b).parents("div:eq(0)").width();
	if (b.offsetWidth > a) {
		b.style.width = a + "px"
	}
}
function forbidMember(a, b) {
	var c = b.value;
	if (!c) {
		return
	}
	b.selectedIndex = 0;
	$.ajax( {
		type : "POST",
		dataType : "json",
		url : "/manage.php",
		data : {
			act : "user",
			op : c,
			userid : a
		},
		success : function(d) {
			alert(d.msg)
		},
		error : function(d) {
			alert(TXT_NETBUSY)
		}
	})
};
